import { _decorator, Component, Node,resources,error, TextAsset} from 'cc';
import { Constant } from './Constant';
const { ccclass, property } = _decorator;

/**
 * 资源工具类
 */
@ccclass('ResourceUtil')
export class ResourceUtil {

    /**
     * 加载资源
     * @param url   资源路径
     * @param type  资源类型
     * @method loadRes
     */
    public static loadRes(url: string, type: any) {
        return new Promise((resolve, reject) => {
            resources.load(url, type, (err: any, res: any) => {
                if (err) {
                    error(err.message || err);
                    reject && reject(err)
                    return;
                }

                resolve && resolve(res);
            })
        })
    }

    /**
     * 获取文本数据
     * @param fileName  文件名
     * @returns 
     */
    public static async getTextData(fileName: string) {
        let content = await this.loadRes(`${Constant.RESOURCES_FILE_NAME.DATA}/` + fileName, null) as TextAsset;
        let text: string = content.text;
        return text;
    }

}


